<?php
/**
 * @file actionList.php
 * @author LD
 * @package news
 * @desc 处理列表
 */
global $module, $action, $nid, $page;
$cats = $dbh->getCategories();

if ($action == 'delete' and ! empty ( $nid )) {
	if ($dbh->deleteNews ( $nid, 'post' ))
		$smarty->assign ( 'success', "DELETE {$nid}" );
	else
		$smarty->assign ( 'error', "DELETE {$nid}" );
}
$smarty->assign ( "page_title", "文章列表" );

if (empty ( $page ))
	$page = 1;
else
	$page = ( int ) $page;

$smarty->assign ( 'page', $page );
$sum = $dbh->getSum ( 'post' );
$sum = intval ( $sum / 10 );
if ($sum % 10 > 0 or $sum == 0)
	$sum ++;

$smarty->assign ( 'sum', $sum );
/*if($page>1) $smarty->assign('prev', $page-1);
else $smarty->assign('next', $page+1);*/
$page = ($page - 1) * 10;

/* 找到同类型的文章  */
$posts = $dbh->getPosts ( $page, " WHERE `type` = '{$module}' " );
//var_dump($posts);

$xPosts = array ();
foreach ( ( array ) $posts as $post ) {
	$xPost = array ('ID' => $post ['ID'] );
	
	foreach ( $cats as $cat ) {
		if ($cat ['ID'] == $post ['category_id']) {
			if ($cat ['type'] == 'help')
				$xPost ['category'] = "<span style=\"color:#6C6;\">{$cat['name']}</span>";
			elseif ($cat ['type'] == 'news')
				$xPost ['category'] = "<span style=\"color:#09C;\">{$cat['name']}</span>";
			elseif ($cat ['type'] == 'job')
				$xPost ['category'] = "<span style=\"color:#C36;\">{$cat['name']}</span>";
			break;
		}
	}
	
	$xPost ['title'] = $post ['title'] . '&nbsp;&nbsp;(' . $post ['date'] . ')';
	$xPost ['edit'] = "<a href=\"entry.php?module={$module}&action=edit&nid={$post['ID']}\">编辑</a>";
	$xPost ['delete'] = "<a href=\"entry.php?module={$module}&action=delete&nid={$post['ID']}\">删除</a>";
	array_push ( $xPosts, $xPost );
}

if (! empty ( $posts )) {
	$smarty->assign ( 'xPosts', $xPosts );
}

$smarty->display ( 'news_postList.html' );
?>